{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7cd88294",
   "metadata": {},
   "source": [
    "----\n",
    "* 记录人：杨光辉\n",
    "* 记录时间：2021-11-15\n",
    "* week12\n",
    "* 主要参考文档：[Python官方文档]（http://python.org/doc）\n",
    "---- \n",
    "## 本周学习内容\n",
    "\n",
    "### 字典的一般方法\n",
    "\n",
    "> 1.如何查询字典  \n",
    "> 2.通过字典的方法实现字典的修改（如：增、删、修改）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "68d2d211",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'> \n",
      " {'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'Liuyu'}\n"
     ]
    }
   ],
   "source": [
    "# 新建字典 dict_name = {key1:valuel , key2:valuel2}\n",
    "stu_info = {\n",
    "    \"stu1801\":\"ZiQi\",\n",
    "    \"stu1802\":\"XiaoFeng\",\n",
    "    \"stu1803\":\"Liuyu\"\n",
    "}\n",
    "print(type(stu_info),'\\n',stu_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7ffc58c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'ZiQi', 'stu1802': 'XiaoHong', 'stu1803': 'Liuyu'}\n"
     ]
    }
   ],
   "source": [
    "# 需要注意的事情：\n",
    "# 如果字典新增的内容的key已经存在？==> 结论： 将会对已有的key对应的value进行修改\n",
    "stu_info['stu1802'] = \"XiaoHong\"\n",
    "print(stu_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0eec635d",
   "metadata": {},
   "outputs": [],
   "source": [
    "> 2.字典的删除\n",
    ">> 1. 字典pop()一般使用方法\n",
    ">>>1.先查寻要删除的key是否存在（避免KeyError的报错）：用 in 去查询成员（key）是否存在\n",
    ">>>2. 然后在删除：D.pop（k[,d]）-> v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "191c2ce4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(...) method of builtins.dict instance\n",
      "    D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n",
      "    If key is not found, d is returned if given, otherwise KeyError is raised\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help (stu_info.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c020c1e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ZiQi'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info.pop(\"stu1801\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "99be908c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1802': 'XiaoHong', 'stu1803': 'Liuyu'}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "79710010",
   "metadata": {
    "code_folding": []
   },
   "outputs": [],
   "source": [
    "# 如果key不存在，会怎么样？ ==> KeyError:'stu1801'\n",
    "# stu_info.pop(\"stu1801\")\n",
    "if \"stu1801\" in stu_info:\n",
    "    stu_info.pop(\"stu1801\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e551ac5b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1802': 'XiaoHong', 'stu1803': 'Liuyu'}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7d9c87f5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
